<?php
include('include/config.inc.php');
if(!isset($_SESSION['s_activId'.$cfgPack])
  || (isset($_SESSION['s_userType'.$cfgPack]) && isset($cfgFileNames['timeslot'][$_SESSION['s_userType'.$cfgPack]]) 
      && $cfgFileNames['timeslot'][$_SESSION['s_userType'.$cfgPack]] == 0))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{	
  $timeSlotDetailArray = array();
  $slotName            = "";
  $i                   = 0;
  $fromTime            = time('H:i:s');
  $toTime              = time('H:i:s');
  $timeSlotId          = isset($_REQUEST['timeSlotId']) ? $_REQUEST['timeSlotId'] : 0;

  // after post : start
  if(isset($_POST['slotName']))
  { 
    if($timeSlotId > 0)
    { 
      $fromTime = $_POST['fromTimeHour'].":".$_POST['fromTimeMinute'].":".$_POST['fromTimeSecond']." ".$_POST['fromTimeMeridian'];   
      $toTime   = $_POST['toTimeHour'].":".$_POST['toTimeMinute'].":".$_POST['toTimeSecond']." ".$_POST['toTimeMeridian'];
      $updateTimeslot = "UPDATE timeslot SET slotName    = '".$_POST['slotName']."',
                                             fromTime    = '".$fromTime."',
                                             toTime      = '".$toTime."'
                                       WHERE timeSlotId  = ".$_REQUEST['timeSlotId'];
      $updateTimeslotRes  = mysql_Query($updateTimeslot);
      if(!$updateTimeslotRes)
        die("Insert Query Not Inserted : ".mysql_error());
      else
        header("Location:timeSlot.php");
    }
    else
    {
      $fromTime = $_POST['fromTimeHour'].":".$_POST['fromTimeMinute'].":".$_POST['fromTimeSecond']." ".$_POST['fromTimeMeridian'];   
      $toTime   = $_POST['toTimeHour'].":".$_POST['toTimeMinute'].":".$_POST['toTimeSecond']." ".$_POST['toTimeMeridian'];   
      $insertQueary= "INSERT INTO timeslot (slotName,fromTime,toTime)
                            VALUE('".$_POST['slotName']."','".$fromTime."','".$toTime."')";
      $timeSlotResult = mysql_query($insertQueary);
      if(!$timeSlotResult)
        die("Insert Query Not Inserted : ".mysql_error());
      else
        header("Location:timeSlot.php");
    }
  }    
  // after post : end
  
  $selectTimeslot = "SELECT timeSlotId,slotName,fromTime,toTime
                       FROM timeslot
                   ORDER BY slotName";
  $selectTimeslotResult = mysql_query($selectTimeslot);
  $itemInRow = mysql_num_rows($selectTimeslotResult);
  while($itemInRow = mysql_fetch_array($selectTimeslotResult))
  {                                                    
    $timeSlotDetailArray[$i]['timeSlotId'] = $itemInRow['timeSlotId'];
    $timeSlotDetailArray[$i]['slotName']   = $itemInRow['slotName'];
    $timeSlotDetailArray[$i]['fromTime']   = $itemInRow['fromTime'];
    $timeSlotDetailArray[$i]['toTime']     = $itemInRow['toTime'];
    if($itemInRow['timeSlotId'] == $timeSlotId)
    {
    	$slotName  = $itemInRow['slotName']; 
    	$fromTime  = $itemInRow['fromTime']; 
    	$toTime    = $itemInRow['toTime']; 
    }
    $i++;     
  }    
   
  include("./bottom.php");
  date_default_timezone_set('Asia/Calcutta');
  $smarty->assign("timeSlotId",$timeSlotId);
  $smarty->assign("slotName",$slotName);
  $smarty->assign("timeSlotDetailArray",$timeSlotDetailArray);
  $smarty->assign("fromTime",$fromTime);
  $smarty->assign("toTime",$toTime);
  $smarty->assign("i",$i);
  $smarty->display("timeSlot.tpl");
}
?>